package com.lg.base_common_library.utils;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Created by ligang on 2017/10/2.
 * description：用于保存app的一些配置信息
 */

public class SPUtils {
    private static SharedPreferences sharedPreferences;
    private static final String SP_FILE_NAME = "config";
    public static boolean getBoolean(Context context, String key, boolean defaultValue) {
        if (sharedPreferences == null) {
            sharedPreferences = context.getSharedPreferences(SP_FILE_NAME, 0);
        }
        return sharedPreferences.getBoolean(key, defaultValue);
    }

    public static int getInt(Context context, String key, int defaultValue) {
        if (sharedPreferences == null) {
            sharedPreferences = context.getSharedPreferences(SP_FILE_NAME, 0);
        }
        return sharedPreferences.getInt(key, defaultValue);
    }

    public static String getString(Context context, String key, String defaultValue) {
        if (sharedPreferences == null) {
            sharedPreferences = context.getSharedPreferences(SP_FILE_NAME, 0);
        }
        return sharedPreferences.getString(key, defaultValue);
    }

    public static void saveBoolean(Context context, String key, boolean value) {
        if (sharedPreferences == null) {
            sharedPreferences = context.getSharedPreferences(SP_FILE_NAME, 0);
        }
        sharedPreferences.edit().putBoolean(key, value).apply();
    }

    public static void saveInt(Context context, String key, int value) {
        if (sharedPreferences == null) {
            sharedPreferences = context.getSharedPreferences(SP_FILE_NAME, 0);
        }
        sharedPreferences.edit().putInt(key, value).apply();
    }

    public static void saveString(Context context, String key, String value) {
        if (sharedPreferences == null) {
            sharedPreferences = context.getSharedPreferences(SP_FILE_NAME, 0);
        }
        sharedPreferences.edit().putString(key, value).apply();
    }
}
